package com.twu.biblioteca;

import org.omg.DynamicAny._DynAnyFactoryStub;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.lang.System.out;

public class Register {

    List<Book> booklist = new ArrayList<Book>();
    List<Book> checkedoutbooklist=new ArrayList<Book>();
    Book book1 = new Book("The Hobbit", null);
    Book book2 = new Book("Harry Potter", null);
    Book book3 = new Book("Castle", null);


    List<Movies> movielist = new ArrayList<Movies>();
    Movies movie1 = new Movies("Robocop", "José Padilha" , 2014, 6);
    Movies movie2 = new Movies("Spider-man", "Sam Raimi" , 2006, 8);
    Movies movie3 = new Movies("Man Of Steel", "Zack Snyder" , 2012, 8);

    Customer customer1 = new Customer("vishwas", "vishwas@gmail.com","8123244329","123-xxxx","vishw@s");
    Customer customer2 = new Customer("Kohli", "kohli@gmail.com","9686122334","124-xxxx","vishw@s");
    Customer customer3 = new Customer("Gayle", "gayle@gmail.com","9797123467","125-xxxx","vishw@s");
    List<Customer> customerlist = new ArrayList<Customer>();

    public Register()
    {
        booklist.addAll(Arrays.asList(book1, book2, book3));
        movielist.addAll(Arrays.asList(movie1, movie2, movie3));
        customerlist.addAll(Arrays.asList(customer1, customer2, customer3));
    }

    public List<Book> getBooklist()
    {
        return booklist;
    }

    public List<Movies> getMovielist()//passed
    {
        return movielist;
    }

    public boolean checkOutBook(String book, String customername)
    {
        for (Book list : booklist)
        {
            if(list.getBookname().equals(book))
            {
                list.setCustomerborrowed(customername);
                if(booklist.remove(list))
                {
                    checkedoutbooklist.add(list);
                    return true;
                }
            }
        }
        return false;
    }

    public boolean checkOutMovie(String movie)
    {
        for (Movies list : movielist)
        {
            if(list.getName().equals(movie))
            {
                if(movielist.remove(list))
                {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean  isCheckedOut(String book)
    {
        for (Book list : checkedoutbooklist)
        {
         if (list.getBookname().equals(book))
             return true;
        }
        return false;
        }

    public void addBook(Book book)
    {
        booklist.add(book);
    }

    public boolean returnBook(String book)

    {
        if(isCheckedOut(book))
        {
            for(Book list: checkedoutbooklist)
            if(list.getBookname().equals(book))
            {
                addBook(list);
            }

            if(removebook(book))
            return true;
            else
            return false;
            }
        else
        {
            return false;
        }
    }

    public boolean removebook(String book)
    {
        for (Book list : checkedoutbooklist)
        {
            if(list.getBookname().equals(book))
            {
                checkedoutbooklist.remove(list);
                return true;
            }
        }
            return false;
    }

    public void displayDetailsOfCustomer(String customername) {

        out.println("The Customer Details are:");
        for (Customer list :customerlist)
        {
        if(customername.equals(list.getName()))
        {
        out.println("Name: " + list.getName());
        out.println("email: " + list.getEmail());
        out.println("phone number: " + list.getPhonenumber());
        }
        }
    }

    public void getCheckedOutBookList() {
        for (Book list : checkedoutbooklist)
        {
            displayallCheckedOutBooks(list);
        }
    }


    private void displayallCheckedOutBooks(Book list) {
        System.out.println(list.getBookname() + list.getCustomerborrowed());
    }



//    private static void BookSelectedByUserForReturn(BibliotecaLibrary library, String bookname) {
//
//        Book book1 = new Book("The Hobbit", null);
//        Book book2 = new Book("Harry Potter", null);
//        Book book3 = new Book("Castle", null);
//        if (bookname.equals("The Hobbit")) {
//            library.BookReturn(book1);
//        }
//        else if (bookname.equals("Harry Potter")) {
//            library.BookReturn(book2);
//        }
//        else if (bookname.equals("Castle")) {
//            library.BookReturn(book3);
//        }
//        else{
//            library.BookReturn(new Book());
//        }
//    }
}
